package marksys.free.com.marksys.ui.user;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import java.util.ArrayList;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.databinding.ActivityRecordBinding;
import marksys.free.com.marksys.ui.adapter.MFragmentPagerAdapter;
import marksys.free.com.marksys.ui.fragment.clean.RecordCleanFragment;
import marksys.free.com.marksys.ui.fragment.discipline.RecordDisciplineFragment;
import marksys.free.com.marksys.ui.fragment.diligent.RecordDiligentFragment;
import marksys.free.com.marksys.ui.main.MainActivity;

public class RecordActivity extends FragmentActivity {
    //实现Tab滑动效果
    private ViewPager mViewPager;
    private ImageView cursor;
    //动画图片偏移量
    private int offset = 0;
    private int position_one;
    private int position_two;
    //当前页卡编号
    private int currIndex = 0;
    //存放Fragment
    private ArrayList<Fragment> fragmentArrayList;
    //管理Fragment
    private FragmentManager fragmentManager;
    public Context context;
    ActivityRecordBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_record);
        binding.recordJie.setTypeface(Typeface.DEFAULT_BOLD);
        binding.recordLv.setTypeface(Typeface.DEFAULT_BOLD);
        binding.recordQin.setTypeface(Typeface.DEFAULT_BOLD);
        binding.backBtn.setOnClickListener(v -> {
            Intent intent = new Intent(RecordActivity.this, MainActivity.class);
            startActivity(intent);
            this.finish();
        });

        context = this;
        binding.recordJie.setOnClickListener(new MyOnClickListener(0));
        binding.recordLv.setOnClickListener(new MyOnClickListener(1));
        binding.recordQin.setOnClickListener(new MyOnClickListener(2));
        //初始化ImageView
        InitImageView();
        //初始化Fragment
        InitFragment();
        //初始化ViewPager
        InitViewPager();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    /**
     * 初始化页卡内容区
     */
    private void InitViewPager() {
        mViewPager = findViewById(R.id.vPager);
        mViewPager.setAdapter(new MFragmentPagerAdapter(fragmentManager, fragmentArrayList));
        //让ViewPager缓存2个页面
        mViewPager.setOffscreenPageLimit(2);
        //设置默认打开第一页
        mViewPager.setCurrentItem(0);
        binding.recordJie.setTextColor(getResources().getColor(R.color.tabText));
        binding.line2.setBackgroundColor(getResources().getColor(R.color.tabText));
        //设置viewpager页面滑动监听事件
        mViewPager.addOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 初始化动画
     */
    private void InitImageView() {
        cursor = findViewById(R.id.cursor);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        // 获取分辨率宽度
        int screenW = dm.widthPixels;
        offset = 0;
        //动画图片偏移量赋值
        position_one = (int) (screenW / 3.0);
        position_two = position_one * 2;
    }

    /**
     * 初始化Fragment，并添加到ArrayList中
     */
    private void InitFragment() {
        fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(new RecordCleanFragment());
        fragmentArrayList.add(new RecordDisciplineFragment());
        fragmentArrayList.add(new RecordDiligentFragment());
        fragmentManager = getSupportFragmentManager();
    }

    /**
     * 头标点击监听
     *
     * @author weizhi
     * @version 1.0
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index;

        MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    /**
     * 页卡切换监听
     *
     * @author weizhi
     * @version 1.0
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            Animation animation = null;
            switch (position) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, 0, 0, 0);
                        resetTextViewTextColor();
                        binding.recordJie.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line2.setBackgroundColor(getResources().getColor(R.color.tabText));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, 0, 0, 0);
                        resetTextViewTextColor();
                        binding.recordJie.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line2.setBackgroundColor(getResources().getColor(R.color.tabText));
                    }
                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_one, 0, 0);
                        resetTextViewTextColor();
                        binding.recordLv.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line5.setBackgroundColor(getResources().getColor(R.color.tabText));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, position_one, 0, 0);
                        resetTextViewTextColor();
                        binding.recordLv.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line5.setBackgroundColor(getResources().getColor(R.color.tabText));

                    }
                    break;
                case 2:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_two, 0, 0);
                        resetTextViewTextColor();
                        binding.recordQin.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line8.setBackgroundColor(getResources().getColor(R.color.tabText));
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, position_two, 0, 0);
                        resetTextViewTextColor();
                        binding.recordQin.setTextColor(getResources().getColor(R.color.tabText));
                        binding.line8.setBackgroundColor(getResources().getColor(R.color.tabText));
                    }
                    break;
            }
            currIndex = position;
            assert animation != null;
            animation.setFillAfter(true);// true:图片停在动画结束位置
            animation.setDuration(300);//动画持续时间，毫秒为单位
            cursor.startAnimation(animation);//对当前View开始设置的Animation动画
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * 将顶部文字恢复默认值
     */
    private void resetTextViewTextColor() {
        binding.recordJie.setTextColor(getResources().getColor(R.color.colorText));
        binding.line2.setBackgroundColor(getResources().getColor(R.color.white));
        binding.recordLv.setTextColor(getResources().getColor(R.color.colorText));
        binding.line5.setBackgroundColor(getResources().getColor(R.color.white));
        binding.recordQin.setTextColor(getResources().getColor(R.color.colorText));
        binding.line8.setBackgroundColor(getResources().getColor(R.color.white));
    }
}
